#include<iostream>
#include<vector>
#include<memory>
using std::endl;
using std::cout;
using std::vector;
using std::allocator;
#include <memory>

template<typename T> 
class Vector 
{ 
public:     
    Vector();     
    ~Vector();          
    void push_back(const T &);      
    void pop_back();         
    int size();     
    int capacity(); 
private:     
    void reallocate();//重新分配内存,动态扩容要用的 
private:         
    static std::allocator<T> _alloc;          
    T *_start;                 //指向数组中的第一个元素     
    T *_finish;                //指向最后一个实际元素之后的那个元素     
    T *_end_of_storage;        //指向数组本身之后的位置 
};
template<typename T> 
std::allocator<T> Vector<T>::_alloc;

template<typename T> 
Vector<T>::Vector()
:_start(nullptr)
,_finish(nullptr)
,_end_of_storage(nullptr){}

template<typename T> 
Vector<T>::~Vector(){
    if(_start){
        for (T* p = _start; p != _end_of_storage; ++p) {
            _alloc.destroy(p);
        }
        _finish = _start;
        _alloc.deallocate(_start, _end_of_storage - _start); 
    }
}
//size
template<typename T>
int Vector<T>::size(){
    return _finish - _start;
}
template<typename T>
int Vector<T>::capacity(){
    return _end_of_storage - _start;
}
template<typename T>
void Vector<T>::pop_back(){
   //做一个判断，如果是开始就没有pop
   if(_finish != _start){
        --_finish;
        _alloc.destroy(_finish);
   }
}

template<typename T>
void Vector<T>::reallocate(){
    //创建多少个初始
    size_t new_capacity = size() == 0 ? 1 : 2 *size();
    T *new_start = _alloc.allocate(new_capacity);
    T *new_finish = new_start;
    T* old_ptr = _start; //久的

    //移动到新的区域
    while(old_ptr != _finish){
       
        _alloc.construct(new_finish,*old_ptr);
        ++new_finish;
        ++old_ptr;
    }
    if(_start){
        while (old_ptr != _start)
        {
            _alloc.destroy(old_ptr);
            --old_ptr;
        }
        _alloc.deallocate(_start,_end_of_storage-_start);
    }
    _start = new_start;
    _finish = new_finish;
    _end_of_storage = _start + new_capacity;
}
template<typename T>
void Vector<T>::push_back(const T &rhs){
    if(_finish == _end_of_storage){
        reallocate();
    }
    _alloc.construct(_finish,rhs);
    ++_finish;
}

int test(){
    Vector<int> test;
    test.push_back(1);
    test.push_back(2);
    cout<< test.capacity()<<endl;
}

int main(void){
    test();
}